import os

img_list = [
    "png",
    "jpeg",
    "jpg",
    "gif",
    "bmp",
    "tif",
    "pcx",
    "tga",
    "exif",
    "fpx",
    "psd",
    "cdr",
    "pcd",
    "dxf",
    "ufo",
    "eps",
    "ai",
    "raw",
    "wmf",
    "webp",
    "avif",
    "apng",
    "heif",
]

video_type = [
    "mp4",
    "mov",
    "wmv",
    "avi",
    "flv",
    "webm",
    "3gp",
    "asf",
    "asx",
    "rm",
    "rmvb",
    "m4v",
    "dat",
    "mkv",
    "vob",
]

audio_type = ["mp3", "wma", "wav", "amr", "m4a", "aac"]

excel_list = ["xls", "xlsx", "csv"]

pdf_list = ['pdf']

font_list = ['ttf', 'otf']

other_list = [
    "doc",
    "docx",
    "ppt",
    "pptx",
    "txt",
    "sav",
    "caj",
    "rar",
    "zip",
    "gzip",
    "json",
]

full_type_list = img_list + video_type + audio_type + other_list + excel_list + pdf_list + font_list

TYPE_LIMIT_LIST = {
    'img': img_list,
    'audio': audio_type,
    'video': video_type,
    'excel': excel_list,
    'pdf': pdf_list,
    'font': font_list,
}


def check_ext(file_name: str, type_limit: [str, None] = None, type_blacklist: [str, None] = None):
    """
    检查上传文件后缀名
    :param file_name: 上传文件名 xxx.txt
    :param type_limit: 文件类型: upload_enums.TYPE_LIMIT_LIST
    :param type_blacklist: 限制文件类型: upload_enums.TYPE_LIMIT_LIST
    :return:
    """

    ext = os.path.splitext(file_name)[-1].strip('.')
    ext_list = TYPE_LIMIT_LIST.get(type_limit, full_type_list)
    blacklist = TYPE_LIMIT_LIST.get(type_blacklist, [])
    if ext.lower() not in ext_list or ext.lower() in blacklist:
        print('文件类型不符合要求')


if __name__ == '__main__':
    filename = 'demo.pdf'
    check_ext(filename, type_blacklist='pdf')